package com.mxbbx.wdn.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mxbbx.wdn.domain.user.User;
/**
 * 用户管理控制类
 * @author 
 *
 */
public class UserServlet extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response)
				throws ServletException,IOException{
				this.doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response)
				throws ServletException,IOException{
			String m = request.getParameter("m");
			if("list".equals(m)){
				process(request,response,"/page/user/user_list.jsp");
			}else if("input".equals(m)){
				process(request,response,"/page/user/user_add.jsp");
			}else if("get".equals(m)){
				process(request,response,"/page/user/user_edit.jsp");
			}else if("query".equals(m)){
				process(request,response,"/page/user/searchUser_list.jsp");
			}else if("add".equals(m)){
				add(request,response);
			}else if("edit".equals(m)){
				edit(request,response);
			}else if("del".equals(m)){
				del(request,response);
			}else{
				error(request,response);
			}
	}

	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		//获取字段值
		String userId=request.getParameter("userId");
		String password=request.getParameter("password");
		String fullName=request.getParameter("fullName");
		String agencode=request.getParameter("agencode");
		String status="1";//默认正常
		String[] roleIds=request.getParameterValues("roleIds");
		//实例化User
		User user=new User(userId,password,fullName,status,agencode);
		//调用添加方法并实现跳转
		try{
			user.addUser(roleIds);
			process(request,response,"/page/user/user_list.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	private void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		String userId=request.getParameter("userId");
		String fullName=request.getParameter("fullName");
		String password=request.getParameter("password");
		String status="1";
		String agencode=request.getParameter("agencode");
		String[] roleIds=request.getParameterValues("roleIds");
		User user=new User(userId,fullName,password,status,agencode);
		try{
			user.editUser(roleIds);
			process(request,response,"/page/user/user_list.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	private void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		String userId=request.getParameter("id");
		User user=new User();
		try{
			System.out.println(userId);
			user.delUser(userId);
			process(request,response,"/page/user/user_list.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}

	}
    
}
